﻿//===========================================================================================================================
// Cuberry - http://www.cuberry.net
// Copyright (c) 2012
// by Peacequare® - Amir Moussa
//===========================================================================================================================
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
// as published by the Free Software Foundation, either version 3 of the License, or any later version.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// This program is distributed in the hope that it will be useful.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//===========================================================================================================================


//added by wli 31-5-2012
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Unity;
using P2.Cuberry.Framework.Helper;
using System;
using P2.Cuberry.Modules.SimpleAccounting.FiTransaction.BusinessEntities;


namespace P2.Cuberry.Modules.SimpleAccounting.FiTransaction
{
    public class FiTransactionFindFiChequePresentationModel : INotifyPropertyChanged
    {
        public FiTransactionFindFiChequePresentationModel(IFiChequeFindView view, IUnityContainer container, IRegionManager regionManager, List<FiCheque> fichquesList, int fiscalYearID)
        {

            this.FiscalYearID = fiscalYearID;
            this.fichquesList = fichquesList.Where(c=>c.FiscalYearID==this.FiscalYearID).Select(c=>c).ToList();          
            View = view;          
            this.FiChequeFindCommand = new DelegateCommand<object>(OnFiAccountFindExecute, OnFiAccountFindCanExecute);
            this.FiChequeSelectCommand = new DelegateCommand<object>(OnFiAccountSelectExecute, OnFiAccountSelectCanExecute);
            this.FiChequeCloseCommand = new DelegateCommand<object>(OnFiAccountCloseExecute, OnFiAccountCloseCanExecute);
            this.FiChequeRefreshCommand = new DelegateCommand<object>(OnFiAccountRefreshExecute, OnFiAccountRefreshCanExecute);

            View.Model = this;
        }


        #region Members
        public IFiChequeFindView View { get; set; }
        public Services.IFiChequeService Service;


        string _FiChequeNoOrBankName = "";
        public string FiChequeNoOrBankName
        {
            get
            {
                return _FiChequeNoOrBankName;
            }
            set
            {
                if (_FiChequeNoOrBankName != value)
                {
                    _FiChequeNoOrBankName = value;
                    OnPropertyChanged("FiChequeNoOrBankName");
                }
            }
        }
        
        string _ChequeNo = "";
        public string ChequeNo
        {
            get
            {
                return _ChequeNo;
            }
            set
            {
                if (_ChequeNo != value)
                {
                    _ChequeNo = value;
                    OnPropertyChanged("ChequeNo");
                }
            }
        }
        string _Bank = "";
        public string Bank
        {
            get
            {
                return _Bank;
            }
            set
            {
                if (_Bank != value)
                {
                    _Bank = value;
                    OnPropertyChanged("Bank");
                }
            }
        }
        
        int _ChequeID = -1;
        public int ChequeID
        {
            get
            {
                return _ChequeID;
            }
            set
            {
                if (_ChequeID != value)
                {
                    _ChequeID = value;
                    OnPropertyChanged("ChequeID");
                }
            }
        }
        private int FiscalYearID { get; set; }

        //Add++
        //added by wli
        public List<BusinessEntities.FiCheque> fichquesList { get; set; }
       
            
            
           

        //end


        #endregion


        #region Commands

        public DelegateCommand<object> FiChequeSelectCommand { get; set; }
        public DelegateCommand<object> FiChequeCloseCommand { get; set; }
        public DelegateCommand<object> FiChequeFindCommand { get; set; }
        public DelegateCommand<object> FiChequeRefreshCommand { get; set; }


        void OnFiAccountSelectExecute(object sourceAccount)
        {
            if (View.SelectedItem != null)
            {
                this.ChequeID  = View.SelectedItem.FiChequeID;
                View.Close(true);
            }
        }


        bool OnFiAccountSelectCanExecute(object sourceAccount)
        {
                return true;
        }


        void OnFiAccountFindExecute(object sourceAccount)
        {
            try
            {
                BusinessEntities.FiCheque objdata = new FiCheque();
                View.UpdateFocusedControl();
                var data = this.fichquesList.Where(c => c.ChequeNo != null && c.Bank != null).Select(c => c);
                this.fichquesList =  data.ToList().Where(c => (c.Bank.ToLower().Contains(FiChequeNoOrBankName.ToLower()) || c.ChequeNo.Value.ToString().ToLower().Contains(FiChequeNoOrBankName.ToLower()) && c.ChequeNo != null && c.Bank != null )).Select(c => c).ToList();               // this.fichquesList = 
                View.Model = this;
            }
            catch (System.Exception ex) //AM:TC
            {
                P2ExceptionHandler.LogException(ex, string.Format("{0}.{1}", this.GetType().Name, System.Reflection.MethodInfo.GetCurrentMethod().Name));
            }
        }


        bool OnFiAccountFindCanExecute(object sourceAccount)
        {
            return true;
        }


        void OnFiAccountCloseExecute(object sourceAccount)
        {
            View.Close(false);
        }


        bool OnFiAccountCloseCanExecute(object sourceAccount)
        {
            return true;
        }


        void OnFiAccountRefreshExecute(object sourceAccount)
        {
            try
            {
                //P2.Cuberry.Modules.SimpleAccounting.FiAccount.BusinessEntities.FiAccount templateAccount = new P2.Cuberry.Modules.SimpleAccounting.FiAccount.BusinessEntities.FiAccount();
                //templateAccount.FiAccountFiscalYearID = this.FiscalYearID;
                //templateAccount.Chargeable = true;
                //this.AvailableAccounts.Clear();
                //this.AvailableAccounts.AddRange(Service.RetrieveFiAccounts(templateAccount).Where(a => a.IsActive).ToList());
                //this.FilteredAccounts = this.AvailableAccounts;
                View.Model = this;
            }
            catch (System.Exception ex) //AM:TC
            {
                P2ExceptionHandler.LogException(ex, string.Format("{0}.{1}", this.GetType().Name, System.Reflection.MethodInfo.GetCurrentMethod().Name));
            }
        }


        bool OnFiAccountRefreshCanExecute(object sourceAccount)
        {
            return true;
        }

        #endregion


       
        

       


        #region Property Changed

        public event PropertyChangedEventHandler PropertyChanged;


        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null) Handler(this, new PropertyChangedEventArgs(propertyName));
        }
        
        #endregion

    }
}
